/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prepocessing_2.com.prep;

import java.util.ArrayList;

/**
 *
 * @author baga
 */
public class Dictionary {

    private ArrayList tokenList;
    private ArrayList docList;
    private boolean write;

    public Dictionary() {
        tokenList = new ArrayList();
        docList = new ArrayList();
    }

    public void setDictionary(String[] tokens, String[] docs) {
//        System.out.println("dictionary tokens : " + tokens.length + " docs : " + docs.length);
        eraseData();
        for (int i = 0; i < tokens.length; i++) {
            if (tokenList.contains(tokens[i])) {
                getPostingList(tokens[i], docs[i]);
            } else {
                addToDictionary(tokens[i], docs[i]);
            }
        }
    }

    public boolean isWriteble() {
        return write;
    }

    private void getPostingList(String token, String doc) {
        String tempDoc = null;
        //System.out.println("masuk getPostingList " +token + " " + doc);
        int indexList = tokenList.lastIndexOf(token);
        tempDoc = (String) docList.get(indexList);
        tempDoc.concat("#" + doc);
        docList.set(indexList, tempDoc);
    }

    private void addToDictionary(String token, String doc) {
        docList.add(doc);
        tokenList.add(token);
    }

    public String[] getDictionary() {
        return (String[]) docList.toArray(new String[docList.size()]);
    }

    public String[] getPosting() {
        return (String[]) tokenList.toArray(new String[tokenList.size()]);
    }

    public void eraseData() {
        docList.clear();
        tokenList.clear();
    }
}
